package com.zzptc.zzsgz.bdsjws.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.fragments.base.BaseFragment;
import com.zzptc.zzsgz.bdsjws.services.MyDownLoadService;
import com.zzptc.zzsgz.bdsjws.vo.VersionInfo;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by zzsgz on 2017/5/16.
 */

public class UpdateVersionFragment extends BaseFragment {

    Unbinder unbinder;
    @BindView(R.id.tv_update)
    TextView tv_Update;
    @BindView(R.id.btn_yes)
    Button btnYes;
    @BindView(R.id.btn_no)
    Button btnNo;
    Unbinder unbinder1;
    VersionInfo versionInfo;
    public String getMsg() {
        Bundle bundle1 = getArguments();
        String msg = bundle1.getString("msg");
        return msg;
    }

    @Override
    public void initDataView(View view) {
        //更新TextView   getArgument
        Bundle bundle = getArguments();
        versionInfo = (VersionInfo) bundle.getSerializable("versioninfo");
      String apkurl=versionInfo.getApkurl();
        if (versionInfo != null) {
            tv_Update.setText(versionInfo.getInfo());
            btnNo.setVisibility(View.VISIBLE);
        } else {
            tv_Update.setText(getMsg());
        }
    }

    @Override
    public int getContentXml() {
        return R.layout.update_version_dialog;//缺少布局文件
    }

    //写一个方法构造UpdateVersionFragment对象  方法的重载
    public static UpdateVersionFragment getUpdateVersionFragment(String msg) {
        UpdateVersionFragment updateVersionFragment = new UpdateVersionFragment();
        Bundle bundle = new Bundle();
        bundle.putString("msg", msg);
        updateVersionFragment.setArguments(bundle);//设置值   传递给Activity
        return updateVersionFragment;
    }

    //写一个方法构造UpdateVersionFragment对象
    public static UpdateVersionFragment getUpdateVersionFragment(VersionInfo versionInfo) {
        UpdateVersionFragment updateVersionFragment = new UpdateVersionFragment();
        Bundle bundle = new Bundle();
        String apkurl=versionInfo.getApkurl();
        bundle.putSerializable("versioninfo", versionInfo);
        updateVersionFragment.setArguments(bundle);//设置值   传递给Activity
        return updateVersionFragment;
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(unbinder!=null){
            unbinder.unbind();
        }
    }


    @OnClick({R.id.btn_yes, R.id.btn_no})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_yes:
//                1、	创 建通知栏，并且要有进度条
//                2、	下载apk
//                下载的地址：http://10.0.2.2:8080/mobileapp/apk/bdsjws-debug.apk
//            3、安装
                //传地址APK在服务器端的地址
                Intent intent=new Intent();
                intent.setClass(getActivity(), MyDownLoadService.class);
                //传字符串过去
                intent.putExtra("apkurl",versionInfo.getApkurl());
                getActivity().startService(intent);

                //  Intent intent1=new Intent(getActivity(),MyDownLoadService.class);
                // getActivity().startService(intent1);

                dismiss();
                break;
            case R.id.btn_no:

                dismiss();
                break;
        }
    }
}
